<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入检测站名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="联系电话" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="isEnable">
        <el-select v-model="queryParams.isEnable" placeholder="是否启用" clearable size="small">
          <el-option v-for=" dict in enableOptions"
                     :key="dict.dictValue"
                     :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="代驾启用" prop="isDjEnable">
        <el-select v-model="queryParams.isDjEnable" placeholder="代驾启用" clearable size="small">
          <el-option v-for=" dict in djOptions"
                     :key="dict.dictValue"
                     :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="自驾启用" prop="isZjEnable">
        <el-select v-model="queryParams.isZjEnable" placeholder="自驾启用" clearable size="small">
          <el-option v-for=" dict in zjOptions"
                     :key="dict.dictValue"
                     :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="站点类型" prop="stype">
        <el-select v-model="queryParams.stype" placeholder="站点类型" clearable size="small">
          <el-option v-for=" dict in stypeOptions"
                     :key="dict.dictValue"
                     :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['station:station:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="stationList" @selection-change="handleSelectionChange">
      <el-table-column label="检测站图片" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="80" :src="ossPreview+ scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="检测站名称" align="center" prop="name"/>
      <el-table-column label="联系电话" align="center" prop="mobile"/>
      <el-table-column label="是否启用" align="center" :formatter="isEnableFormat" prop="isEnable"/>
      <el-table-column label="代驾启用" align="center" :formatter="isDjEnableFormat" prop="isDjEnable"/>
      <el-table-column label="自驾启用" align="center" :formatter="isZjEnableFormat" prop="isZjEnable"/>
      <el-table-column label="站点类型" align="center" :formatter="stypeFormat" prop="stype"/>
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" >
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUnBind(scope.row)"
                  v-hasPermi="['station:station:edit']"
                >解绑
                </el-button>
              </el-dropdown-item>

            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--检测站列表-->
    <el-dialog title="检测站列表" :visible.sync="open" width="80%" v-if="open" append-to-body>
      <station-add :agentId="agentId"></station-add>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="stationBindConfirm">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {listStation, getStation, addStation, updateStation} from "@/api/station/station";
  import {unbindStation} from "@/api/agent/agent";
  import defaultSettings from '@/settings';
  import StationAdd from './stationAdd'

  export default {
    name: "Station",
    props: {
      agentId: {
        type: Number
      }
    },
    components: {StationAdd},
    data() {
      return {
        ossPreview: defaultSettings.ossPreview,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 检测站表格数据
        stationList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        priceopen: false,
        driveropen: false,
        orderopen: false,
        useropen: false,
        destroy: true,
        stationId:0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          mobile: undefined,
          isEnable: undefined,
          isDjEnable: undefined,
          isZjEnable: undefined,
          stype: undefined,
          agentId: this.agentId
        },
        // 状态数据字典
        enableOptions: [],
        djOptions: [],
        zjOptions: [],
        stypeOptions: [],
        areaoptions: [],
        postForm: {
          detailedAddress: '',
          longitude: '',
          latitude: ''
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_is_enable").then(response => {
        this.enableOptions = response.data;
        this.djOptions = response.data;
        this.zjOptions = response.data;
      });
      this.getDicts("station_stype").then(response => {
        this.stypeOptions = response.data;
      });
    },
    methods: {
      /** 查询检测站列表 */
      getList() {
        this.loading = true;
        listStation(this.queryParams).then(response => {
          this.stationList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.open = true;
      },
      stationBindConfirm(){
        this.open = false;
        this.getList();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getStation(id).then(response => {
          this.form = response.data;
          this.areaArr[0] = this.form.provinceCode;
          this.areaArr[1] = this.form.cityCode;
          this.areaArr[2] = this.form.regionCode;
          this.open = true;
          this.title = "修改检测站";
          var coors = this.form.coordinate.split(",");
          this.postForm.longitude = coors[0];
          this.postForm.latitude = coors[1];
          this.postForm.detailedAddress = this.form.locPoi;
        });
      },
      handlePrice(row) {
        this.priceopen = true;
        this.stationId = row.id;
        this.title = "检测站价格体系";
      },
      handleDriver(row) {
        this.driveropen = true;
        this.stationId = row.id;
        this.title = "驾驶员列表";
      },

      handleOrder(row) {
        this.orderopen = true;
        this.stationId = row.id;
        this.title = "检测站订单";
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.coordinate = location.lng + "," + location.lat;
            this.form.coordinate = this.postForm.longitude + "," + this.postForm.latitude;
            this.form.locPoi = this.postForm.detailedAddress;


            if (this.form.id != undefined) {
              updateStation(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addStation(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },

      /** 解绑按钮操作 */
      handleUnBind(row) {
        let form = {
          id: this.agentId,
          stationId: row.id
        }
        const ids = row.id || this.ids;
        this.$confirm('是否确认解除绑定', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return unbindStation(form);
        }).then(() => {
          this.getList();
          this.msgSuccess("解绑成功");
        }).catch(function () {
        });
      },

      getPosition({address, lat, lng, name}) {
        this.postForm.detailedAddress = address;
        this.postForm.longitude = String(lng);
        this.postForm.latitude = String(lat);
      },
      handleAddressFun: function(value){
        console.log(value)
        var node = this.$refs['cascaderAddr'].getCheckedNodes()[0];
        var  thsAreaCode = node.pathLabels   //注意2： 获取label值
        console.log(thsAreaCode)
        this.form.provinceCode = value[0];
        this.form.cityCode = value[1];
        this.form.regionCode = value[2];
        this.form.province = thsAreaCode[0];
        this.form.city = thsAreaCode[1];
        this.form.region = thsAreaCode[2];
      },
      isEnableFormat(row, column) {
        return this.selectDictLabel(this.enableOptions, row.isEnable);
      },
      isDjEnableFormat(row, column) {
        return this.selectDictLabel(this.djOptions, row.isDjEnable);
      },
      isZjEnableFormat(row, column) {
        return this.selectDictLabel(this.zjOptions, row.isZjEnable);
      },
      stypeFormat(row, column) {
        return this.selectDictLabel(this.stypeOptions, row.stype);
      }
    }

  };
</script>
<style>
  .expand-item {
    margin-top: 15px;
  }
</style>
